//
//  oldMomentsTableViewCell.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/4/2.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMomentsTableViewCell.h"
#import "UIImageView+oldImageView.h"
#import "oldNetWork.h"
#import "oldHub.h"
#import "oldRatioImageView.h"

@interface oldMomentsTableViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *img_avatar;
@property (weak, nonatomic) IBOutlet UILabel *label_name;
@property (weak, nonatomic) IBOutlet UILabel *label_time;
@property (weak, nonatomic) IBOutlet UILabel *label_content;
@property (weak, nonatomic) IBOutlet UIView *view_comment;
@property (weak, nonatomic) IBOutlet UILabel *label_comment;
@property (weak, nonatomic) IBOutlet UIView *view_thumb;
@property (weak, nonatomic) IBOutlet UILabel *label_thumb;
@property (weak, nonatomic) IBOutlet oldRatioImageView *img1;
@property (weak, nonatomic) IBOutlet oldRatioImageView *img2;
@property (weak, nonatomic) IBOutlet oldRatioImageView *img3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgView_height;
@property (weak, nonatomic) IBOutlet UIButton *btn_thumb;

@end

@implementation oldMomentsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _img_avatar.layer.cornerRadius = 35/2;
    UITapGestureRecognizer *tapThumb=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapThumb:)];
    [_view_thumb addGestureRecognizer:tapThumb];
}

- (void)tapThumb:(id)sender
{
    WeakSelf
    
    if (![_moment.isLike boolValue]) {
        [oldHub waiting:@"加载中.."];
        [OLDNetWork httpRequestWithPostPut:URL_Feed_Like params:@{@"fid":_moment.fid} success:^(id data) {
            [oldHub hideHUD];
            weakSelf.moment.isLike = @"true";
            NSInteger number = [weakSelf.moment.likeCount integerValue] + 1;
            weakSelf.moment.likeCount = [NSString stringWithFormat:@"%ld", (long)number];
            weakSelf.label_thumb.text = [NSString stringWithFormat:@"点赞(%@)", weakSelf.moment.likeCount];
            [weakSelf.btn_thumb setImage:[UIImage imageNamed:@"已点赞"] forState:UIControlStateNormal];
        } error:^(NSString *error) {
            [oldHub warningUser:error];
        }];
    }
    else
    {
        [oldHub waiting:@"加载中.."];
        [OLDNetWork httpRequestWithPostPut:URL_Feed_Unlike params:@{@"fid":_moment.fid} success:^(id data) {
            [oldHub hideHUD];
            weakSelf.moment.isLike = @"false";
            NSInteger number = [weakSelf.moment.likeCount integerValue] - 1;
            weakSelf.moment.likeCount = [NSString stringWithFormat:@"%ld", (long)number];
            weakSelf.label_thumb.text = [NSString stringWithFormat:@"点赞(%@)", weakSelf.moment.likeCount];
            [weakSelf.btn_thumb setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
        } error:^(NSString *error) {
            [oldHub warningUser:error];
        }];
    }
}

- (void)setMoment:(oldMoment *)moment
{
    _moment = moment;
    [_img_avatar showHeadImg:moment.avatar];
    _label_name.text = moment.uname;
    _label_time.text = moment.created_at;
    _label_thumb.text = [NSString stringWithFormat:@"点赞(%@)", moment.likeCount];
    _label_comment.text = [NSString stringWithFormat:@"评论(%@)", moment.commentCount];
    _label_content.text = moment.content;
    
    if ([moment.isLike boolValue]) {
        [_btn_thumb setImage:[UIImage imageNamed:@"已点赞"] forState:UIControlStateNormal];
    }
    else
    {
        [_btn_thumb setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
    }
    
    [self initPicView];
}

- (void)initPicView
{
    if ([_moment getPicArray]) {
        _imgView_height.constant = 82.f;
        NSArray *array = [_moment getPicArray];
        switch ([array count]) {
            case 1:
            {
                _img1.hidden = NO;
                _img2.hidden = YES;
                _img3.hidden = YES;
                [_img1 initWithUrl:array[0]];
            }
                break;
            case 2:
            {
                _img1.hidden = NO;
                _img2.hidden = NO;
                _img3.hidden = YES;
                [_img1 initWithUrl:array[0]];
                [_img2 initWithUrl:array[1]];
            }
                break;
            case 3:
            {
                _img1.hidden = NO;
                _img2.hidden = NO;
                _img3.hidden = NO;
                [_img1 initWithUrl:array[0]];
                [_img2 initWithUrl:array[1]];
                [_img3 initWithUrl:array[2]];
            }
                break;
            default:
                break;
        }
    }
    else
    {
        _imgView_height.constant = 0.f;
        _img1.hidden = YES;
        _img2.hidden = YES;
        _img3.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

@end
